package com.example.smartrefreshlayoutdemo.adapter;

import android.content.res.Resources;
import android.util.TypedValue;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.smartrefreshlayoutdemo.R;

public class SmartViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    private final AdapterView.OnItemClickListener mListener;
    private int mPosition = -1;

    public SmartViewHolder(@NonNull View itemView, AdapterView.OnItemClickListener mListener) {
        super(itemView);
        this.mListener = mListener;

        itemView.setOnClickListener(this);
        if (itemView.getBackground() == null) {
            TypedValue typedValue = new TypedValue();
            Resources.Theme theme = itemView.getContext().getTheme();
            int top = itemView.getPaddingTop();
            int bottom = itemView.getPaddingBottom();
            int left = itemView.getPaddingLeft();
            int right = itemView.getPaddingRight();
            if (theme.resolveAttribute(R.attr.selectableItemBackground,typedValue,true)) {
                itemView.setBackgroundResource(typedValue.resourceId);
            }
            itemView.setPadding(left,top,right,bottom);
        }
    }

    public void setPosition(int position){
        this.mPosition = position;
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            int position = getAdapterPosition();
            if (position >= 0) {
                mListener.onItemClick(null,v,position,getItemId());
            }else if(position > -1){
                mListener.onItemClick(null,v,position,getItemId());
            }
        }
    }

    private View findViewById(int id){
        if (id == 0) {
            return itemView;
        }else{
            return itemView.findViewById(id);
        }
    }

    public SmartViewHolder text(int id,CharSequence sequence){
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(sequence);
        }
        return this;
    }

    public SmartViewHolder text(int id, @StringRes int stringRes){
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setText(stringRes);
        }
        return this;
    }

    public SmartViewHolder textColorId(int id,int colorId){
        View view = findViewById(id);
        if (view instanceof TextView) {
            ((TextView) view).setTextColor(ContextCompat.getColor(view.getContext(),colorId));
        }
        return this;
    }

    public SmartViewHolder image(int id,int imageId){
        View view = findViewById(id);
        if (view instanceof ImageView) {
            ((ImageView) view).setImageResource(imageId);
        }
        return this;
    }

    public SmartViewHolder gone(int id){
        View view = findViewById(id);
        if (view != null) {
            view.setVisibility(View.GONE);
        }
        return this;
    }

    public SmartViewHolder visible(int id){
        View view = findViewById(id);
        if (view != null) {
            view.setVisibility(View.VISIBLE);
        }
        return this;
    }
}
